#include "ZPackReactor.h"


ZTcpPackReactorMgr::~ZTcpPackReactorMgr()
{
	for (auto i=m_reactorMap.begin(); i!=m_reactorMap.end(); i++)
	{
		delete i->second;
	}
	m_reactorMap.clear();
}

void ZTcpPackReactorMgr::DealPackage( const ZPackageBase& pack, QTcpSocket* pDealSock ) const
{
	const QString& sPackType = pack.GetPackageType();

	auto rangeIt = m_reactorMap.equal_range(sPackType);

	for (auto i=rangeIt.first; i!=rangeIt.second; i++)
	{
		i->second->DealPackage(pack, pDealSock);
	}
}

void ZTcpPackReactorMgr::RegReactor( ZTcpPackReactor* pReactor )
{
	QString sType = pReactor->GetCarePackType();
	m_reactorMap.insert(ReactorMap::value_type(sType, pReactor));
}

void ZTcpPackReactorMgr::UnRegReactor( ZTcpPackReactor* pReactor )
{
	for (auto i=m_reactorMap.begin(); i!=m_reactorMap.end();)
	{
		if (i->second == pReactor)
		{
			delete pReactor;
			i == m_reactorMap.erase(i);
		}
		else
		{
			i++;
		}
	}
}

void ZTcpPackReactorMgr::UnRegReactor( const QString& reactorType )
{
	for (auto i=m_reactorMap.begin(); i!=m_reactorMap.end();)
	{
		if (i->first == reactorType)
		{
			delete i->second;
			i == m_reactorMap.erase(i);
		}
		else
		{
			i++;
		}
	}
}
